<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Category Class
 *
 * @category	Home
 */
class CategoryAction extends HomeAction
{

	protected $Category;

	public function _initialize()
	{
		parent::_initialize();
		$this->Category = D('Category');
	}


	//分类问题列表
	public function index()
    {
    	
    	$cateID = isset($_REQUEST['cid']) ? intval($_REQUEST['cid']) : 0;
    	$categories = F('cache_categories');
    	$category = $seo = $map = array();

    	if ($cateID && !isset($categories[$cateID]))
    	{
    		$this->error(L('category_not_existed'));
    	}

    	if ($cateID)
    	{
    		$category = $categories[$cateID];
    		$seo = array(
	    		'title' => $category['name'],
	    		'keywords' => $category['keywords'],
	    		'description' => $category['description'],
	    	);
    	}

    	if (!empty($category['redirect']))
    	{
    		redirect($category['redirect']);
    	}

    	$isUltimate = is_ultimate_category($cateID);
    	$map['parent'] = $isUltimate ? $category['parent'] : $cateID;
    	$order = array(
    		'display_order' => 'asc', 
    		'name' => 'asc',
    	);

    	$subCategories = $this->Category->where($map)->order($order)->select();
    	$breadCrumbs = get_category_path($cateID);

    	$map = $order = array();
    	$cateIDs = $parameter = '';

    	/*-------------------- 附加条件 ---------------------*/
    	if ($cateID)
    	{
    		$cateIDs = get_sub_category_ids($cateID, 1);
    		$map['category_id'] = array('in', $cateIDs);
    		$parameter .= '&cid='.$cateID;
    	}

    	$status = isset($_GET['status']) ? strtolower($_GET['status']) : 'all';
		switch ($status)
		{
		    case 'all':
		        break;
			case 'unanswered':
				$map['status'] = 1;
				$map['Question.answer_count'] = 0;
				break;
			case 'open':
			    $map['status'] = 1;
			    break;
			case 'voting':
			    $map['status'] = 2;
			    break;
			case 'resolved':
			    $map['status'] = 3;
			    break;
			case 'reward':
			    $map['is_reward'] = 1;
			    break;
			case 'urgent':
			    $map['is_urgent'] = 1;
			    break;
		}

		$parameter .= '&status='.$status;
    	$map['is_visible'] = 1;

    	/*------------------- 排序 ---------------------*/
		$sort = isset($_GET['sort']) ? intval($_GET['sort']) : -1;
        switch (intval($sort))
        {
            case 1: //按时间排序
                $order['question_id'] = 'asc';
                break;
        	case -1:
        		$order['question_id'] = 'desc';
        		break;
        	case 2: //按star数排序
        	    $order['star_count'] = 'asc';
        	    break;
        	case -2:
        	    $order['star_count'] = 'desc';
        	    break;
        	case 3: //按答案数排序
        	    $order['answer_count'] = 'asc';
        	    break;
        	case -3:
        	    $order['answer_count'] = 'desc';
        	    break;
        }

        $parameter .= '&sort='.$sort;

    	$pageSize = $this->_global['questions_per_page'];
		$maxPage  = $this->_global['max_question_pages'];
		if ($maxPage && isset($_GET[C('VAR_PAGE')]) && intval($_GET[C('VAR_PAGE')]) > $maxPage)
		{
    		$_GET[C('VAR_PAGE')] = $maxPage;
    	}

		import('ORG.Util.Page');

		$View = D('QuestionView');

    	$total = $View->where($map)->count();
    	$Page  = new Page($total, $pageSize);
		$Page->parameter = $parameter;
		$pagination = $Page->show();
    	$questions  = $View->where($map)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('seo', $seo);
    	$this->assign('cateID', $cateID);
    	$this->assign('status', $status);
    	$this->assign('sort', $sort);
    	$this->assign('breadCrumbs', $breadCrumbs);
    	$this->assign('category', $category);
    	$this->assign('subCategories', $subCategories);
    	$this->assign('questions', $questions);
    	$this->assign('pagination', $pagination);

    	$this->display();
    }

}
// End  Class

/* End of file CategoryAction.class.php */
/* Location: /var/www/qqas/App/Lib/Action/Home/CategoryAction.class.php */